package br.com.lucasddaniel.inbuzios.activities;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import br.com.lucasddaniel.inbuzios.Controller;
import br.com.lucasddaniel.inbuzios.R;
import br.com.lucasddaniel.inbuzios.bean.ResponsavelBean;
import br.com.lucasddaniel.inbuzios.dao.ExcessaoBanco;

public class LoginActivity extends Activity implements OnClickListener {

    private EditText m_edttext_senha;
    private EditText m_edttext_login;
    private Button m_btn_entrar;
    private final Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initComponents();
    }

    private void initComponents() {
        m_btn_entrar = (Button) findViewById(R.id_activity_login.btn_entrar);
        m_edttext_login = (EditText) findViewById(R.id_activity_login.edttext_login);
        m_edttext_senha = (EditText) findViewById(R.id_activity_login.edttext_senha);
        if (! Controller.isVersaoproducao()) {
        	m_edttext_login.setText(getString(R.string.activity_login_data_login));
        	m_edttext_senha.setText(getString(R.string.activity_login_data_senha));
        }

        m_btn_entrar.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id_activity_login.btn_entrar:
                String senhaDigitada = m_edttext_senha.getText().toString();
                String usuarioDigitado = m_edttext_login.getText().toString();

                String loginAdm = getResources().getString(R.string.activity_login_data_login);
                String senhaAdm = getResources().getString(R.string.activity_login_data_senha);
                ResponsavelBean rbAdm = new ResponsavelBean(loginAdm, senhaAdm);
                ResponsavelBean rbDigitado = new ResponsavelBean(usuarioDigitado, senhaDigitada);

                ResponsavelBean result = null;
                try {
                    result = Controller.getInstance().buscarResponsavel(rbDigitado, getApplicationContext());
                } catch (ExcessaoBanco e) {
                    e.printStackTrace();
                }

                Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
                if (usuarioDigitado.equals("") || senhaDigitada.equals("")) {
                    m_edttext_login.startAnimation(shake);
                    m_edttext_senha.startAnimation(shake);
                    Toast.makeText(this, getResources().getString(R.string.activity_login_error_msg1), Toast.LENGTH_SHORT).show();
                } else if (rbAdm.isSameLogin(usuarioDigitado, senhaDigitada)) {
                    Intent i = new Intent(this, MenuActivity.class);
                    i.putExtra("showMenu", true);
                    Controller.getInstance().setResponsavelLogado(rbAdm);

                    startActivity(i);
                } else if (result != null) {
                    Intent i = new Intent(this, MenuActivity.class);
                    i.putExtra("showMenu", false);
                    Controller.getInstance().setResponsavelLogado(rbAdm);
                    startActivity(i);
                } else {
                    m_edttext_login.startAnimation(shake);
                    m_edttext_senha.startAnimation(shake);
                    Toast.makeText(this, getResources().getString(R.string.activity_login_error_msg2), Toast.LENGTH_SHORT).show();
                }

                break;

            default:
                break;
        }
    }

    protected void onResume() {
        super.onResume();
        File dir = getCacheDir(); 
        if (dir != null && dir.isDirectory()) { 
        	deleteDir(dir); 
        } 
    }

	public static void deleteDir(File dir) {
		String[] children = dir.list();
		for (int i = 0; i < children.length; i++) {
			new File(dir, children[i]).delete();
		}
	}
 
    /**
     * Exibe uma mensagem ao usuario de acordo com os parametros passados. Usada em excessoes.
     * 
     * @param titulo
     * @param message
     */
    protected void notificarUsuario(final String titulo, final String message) {
        mHandler.post(new Runnable() {
            public void run() {
                new AlertDialog.Builder(LoginActivity.this).setTitle(titulo).setMessage(message).create().show();
            }
        });
    }
}
